#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

// int* productExceptSelf(int* nums, int numsSize, int* returnSize)
// {
//     int i=0;
//     int* answer=(int*)malloc(numsSize*sizeof(int));
//     for(i=0;i<numsSize;i++)
//     {
//         answer[i]=1;

//         int j=0;
//         for(j=0;j<numsSize;j++)
//         {
//             if(j!=i)
//             {
//                 answer[i]*=nums[j];
//             }
//         }
//     }
//     *returnSize = numsSize;

//     return answer;
// }

int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
    int* Larr = (int*)malloc(numsSize * sizeof(int));
    int* Rarr = (int*)malloc(numsSize * sizeof(int));

    Larr[0] = 1;
    int i = 0;
    for (i = 1; i < numsSize; i++)
    {
        Larr[i] = nums[i - 1] * Larr[i - 1];
    }

    Rarr[numsSize - 1] = 1;
    for (i = numsSize - 1 - 1; i >= 0; i--)
    {
        Rarr[i] = nums[i + 1] * Rarr[i + 1];
    }

    int* answer = (int*)malloc(numsSize * sizeof(int));
    for (i = 0; i < numsSize; i++)
    {
        answer[i] = Larr[i] * Rarr[i];
    }

    *returnSize = numsSize;
    free(Larr);
    free(Rarr);

    return answer;
}

int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
    int* Larr = (int*)malloc(numsSize * sizeof(int));

    Larr[0] = 1;
    int i = 0;
    for (i = 1; i < numsSize; i++)
    {
        Larr[i] = nums[i - 1] * Larr[i - 1];
    }

    int right = 1;
    for (i = numsSize - 1 - 1; i >= 0; i--)
    {
        right *= nums[i + 1];
        Larr[i] *= right;
    }

    *returnSize = numsSize;

    return Larr;
}

int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
    int* answer = (int*)malloc(numsSize * sizeof(int));
    int i = 0;
    for (i = 0; i < numsSize; i++)
    {
        answer[i] = 1;
    }

    int left = 1;
    int right = 1;
    for (i = 1; i < numsSize; i++)
    {
        left *= nums[i - 1];
        answer[i] *= left;

        right *= nums[numsSize - i];
        answer[numsSize - 1 - i] *= right;
    }

    *returnSize = numsSize;

    return answer;
}